package com.yricky.indesk.ui.compose.page

import androidx.compose.foundation.layout.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.yricky.indesk.R
import com.yricky.indesk.ui.compose.*
import com.yricky.indesk.ui.dialog.showComposeDialog
import com.yricky.indesk.utils.asIntent
import com.yricky.indesk.utils.callUninstall
import com.yricky.indesk.viewmodel.MainActivityViewModel

/**
 * @author Yricky
 * @date 2021/12/26
 */

/**
 * 完整描述MainActivity视图的函数
 */
@Composable
fun MainActivityPage(viewModel: MainActivityViewModel){
    val viewState by viewModel.viewState.collectAsState()
    val context = LocalContext.current
    Column(
        Modifier
            .fillMaxSize()
            .padding(8.dp)) {
        //Spacer(modifier = Modifier.height(200.dp))
        MainGridAppList(modifier = Modifier
            .fillMaxSize()
            .weight(1f),
            viewState.config.row,viewState.config.column,
            viewState.config.pages[viewState.index],
            onClick = { it,_->
                try{
                    context.startActivity(it.asIntent())
                }catch (e:Throwable){  }
            },
            onLongClick = { entrance,index->
                showComposeDialog(context){
                    Column {
                        AppItem(modifier = Modifier.wrapContentSize(), entrance = entrance)
                        Column(modifier = Modifier.padding(4.dp)){
                            Row {
                                EInkTextButton(
                                    modifier = Modifier
                                        .padding(4.dp)
                                        .weight(1f),
                                    text = "卸载"
                                ){
                                    it.context.callUninstall(entrance)
                                    it.dismiss()
                                }
                                EInkTextButton(
                                    modifier = Modifier
                                        .padding(4.dp)
                                        .weight(1f),
                                    text = "移除"
                                ){
                                    viewModel.onAction(MainActivityViewModel.Action.RemoveEntrance(index))
                                    it.dismiss()
                                }
                            }
                        }

                    }
                }
            }
        )
        BottomBar(modifier = Modifier
            .fillMaxWidth()
            .padding(0.dp, 2.dp, 0.dp, 0.dp),
            text = viewState.bottomBarText
        ){
            SettingsIconButton()
            PageIndicator(
                index = viewState.index,
                count = viewState.config.pages.size
            ){
                viewModel.onAction(MainActivityViewModel.Action.FlipTo(it))
            }
        }
    }
}